﻿using Ecommerce.Areas.Manager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ecommerce.Common;

namespace Ecommerce.Areas.Manager.Controllers
{
    public class SettingController : Controller
    {
        //
        // GET: /Manager/Setting/

        private DatabaseContext db = new DatabaseContext();

        public ActionResult AboutUs()
        {
            var model = db.Settings.Where(x => x.SettingType == (int)EcommerceEnum.SettingType.AboutUs).FirstOrDefault();
            if (model == null)
            {
                model = new Setting();
                model.SettingType = (int)EcommerceEnum.SettingType.AboutUs;
            }

            ViewBag.Title = "Setting content of About us";

            return View(model);
        }

        public ActionResult SpecialOffers()
        {
            var model = db.Settings.Where(x => x.SettingType == (int)EcommerceEnum.SettingType.SpecialOffers).FirstOrDefault();
            if (model == null)
            {
                model = new Setting();
                model.SettingType = (int)EcommerceEnum.SettingType.SpecialOffers;
            }

            ViewBag.Title = "Setting content of Special Offers";

            return View("AboutUs", model);
        }

        public ActionResult HowToOrder()
        {
            var model = db.Settings.Where(x => x.SettingType == (int)EcommerceEnum.SettingType.HowToOrder).FirstOrDefault();
            if (model == null)
            {
                model = new Setting();
                model.SettingType = (int)EcommerceEnum.SettingType.HowToOrder;
            }

            ViewBag.Title = "Setting content of How to Order";

            return View("AboutUs", model);
        }

        public ActionResult PaymentMethod()
        {
            var model = db.Settings.Where(x => x.SettingType == (int)EcommerceEnum.SettingType.PaymentMethod).FirstOrDefault();
            if (model == null)
            {
                model = new Setting();
                model.SettingType = (int)EcommerceEnum.SettingType.PaymentMethod;
            }

            ViewBag.Title = "Setting content of Payment Method";

            return View("AboutUs", model);
        }

        public ActionResult BuyUnique()
        {
            var model = db.Settings.Where(x => x.SettingType == (int)EcommerceEnum.SettingType.BuyUnique).FirstOrDefault();
            if (model == null)
            {
                model = new Setting();
                model.SettingType = (int)EcommerceEnum.SettingType.BuyUnique;
            }

            ViewBag.Title = "Setting content of Buy Unique";

            return View("AboutUs", model);
        }

        [HttpPost]
        public ActionResult Save(Setting model)
        {
            var st = db.Settings.Where(x => x.SettingType == model.SettingType).FirstOrDefault();
            if (st == null)
            {
                model.LastUpdate = DateTime.Now;
                db.Settings.Add(model);
            }
            else
            {
                st.LastUpdate = DateTime.Now;
                st.Content = model.Content;
                db.Entry(st).State = System.Data.EntityState.Modified;
            }

            db.SaveChanges();

            return RedirectToAction(Enum.GetName(typeof(EcommerceEnum.SettingType), st.SettingType));
        }
    }
}
